// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.


function click(e) {
    chrome.tabs.executeScript(null,
        {
            code:"document.body.style.backgroundColor='" + e.target.id + "'"});
    window.close();
}

document.addEventListener('DOMContentLoaded', function () {
    var divs = document.querySelectorAll('div');
    for (var i = 0; i < divs.length; i++) {
        divs[i].addEventListener('click', click);
    }
    document.getElementById("settings").onclick =  function() {
        chrome.tabs.create({url: chrome.extension.getURL("settingsPage.html")});

    }

    // fesh jquery
});

// use this code to add some url there
function addSite() {
    chrome.tabs.getSelected(null, function(tab) {
        var sitesStr = localStorage["sites"];
        sitesStr = sitesStr || "[]";
        var sites = JSON.parse(sitesStr);
        var url = tab.url;
        url = url.replace(/.*:\/\//ig, "");
        url = url.replace(/\/.*/ig, "");
        if(sites.indexOf(url) == -1) {
            sites.push(url);
        }
        localStorage["sites"] = JSON.stringify(sites);
    });

    document.getElementById("added").style.display = "inline-block";

};



setInterval(function() {
    var lastPopUpTime = localStorage['time']; // last time popUp
    var timeBetweenPopUps = new Number(localStorage['timePopUps']) * 60 * 1000; // time in minutes between popups

    var timePassed = new Date().getTime() - lastPopUpTime;
    var timeRemaining = Math.max(0, timeBetweenPopUps - timePassed);

    var timerRemainingSeconds = parseInt(timeRemaining / 1000) % 60;
    var timerRemainsMinutes = parseInt(timeRemaining / 1000 / 60) % 60;

    document.getElementById("secondsFirstDigit").innerHTML = timerRemainingSeconds%10;
    document.getElementById("secondsSecondDigit").innerHTML = parseInt(timerRemainingSeconds/ 10) % 10;

    document.getElementById("minutesFirstDigit").innerHTML = timerRemainsMinutes%10;
    document.getElementById("minutesSecondDigit").innerHTML = parseInt(timerRemainsMinutes/ 10) % 10;

},1000)


